//
//  HLB_EditMerchatVC.m
//  dyseller
//
//  Created by 红喇叭 on 2017/12/5.
//  Copyright © 2017年 红喇叭. All rights reserved.
//

#import "HLB_EditMerchatVC.h"

@interface HLB_EditMerchatVC ()<UIActionSheetDelegate,UITextFieldDelegate>{
    NSString *_imageurl;
}
@property (weak, nonatomic) IBOutlet UILabel *merchatNameLab;
@property (weak, nonatomic) IBOutlet UILabel *updateLab;
@property (weak, nonatomic) IBOutlet UITextField *merchatNameField;
@property (weak, nonatomic) IBOutlet UIImageView *showCoverImg;
@property (weak, nonatomic) IBOutlet UIButton *delButton;

@end

@implementation HLB_EditMerchatVC

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *title = _isNew?@"新增相册":@"编辑相册";
    [self setNavTitle:title titleColor:kBlackColor font:font17];
    
    [self operateView];
    [self rightNavItem];
}

- (void)operateView{
    _merchatNameLab.font = font16;
    _updateLab.font = font16;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chooseImage)];
    _showCoverImg.userInteractionEnabled = YES;
    [_showCoverImg addGestureRecognizer:tap];
    if(!_isNew && _editDataModel){
        _merchatNameField.text = _editDataModel.album_name;
        [_showCoverImg sd_setImageWithURL:[NSURL URLWithString:_editDataModel.album_cover] placeholderImage:[UIImage imageNamed:@"photo_bg12_default"]];
        _imageurl = _editDataModel.album_cover;
        _delButton.hidden = NO;
    }else{
        _delButton.hidden = YES;
    }
    _merchatNameField.delegate = self;
    _delButton.layer.borderColor = MianRedColor.CGColor;
    _delButton.layer.borderWidth = 0.5;
    _delButton.layer.cornerRadius = 5;
    _delButton.layer.masksToBounds = YES;
    [_delButton setTitleColor:MianRedColor forState:UIControlStateNormal];
    
}

- (void)rightNavItem{
    
    __weak typeof (self)weakself = self;
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [rightBtn setTitle:_isNew?@"新建":@"保存" forState:UIControlStateNormal];
    [rightBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
    [rightBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^(UIButton* sender) {
        [weakself saveMerchatAction];
    }];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
}

#pragma  mark ========网络请求========
- (void)saveMerchatAction{
    [self.view endEditing:YES];
    if(_merchatNameField.text.length<1){
         [AlertUtils showToastWithMessage:@"请填写相册名称" inView:self.view hideAfterDelay:2.0 beIndeter:NO];
        return;
    }
    if(_imageurl.length<1){
        [AlertUtils showToastWithMessage:@"请先选择图片" inView:self.view hideAfterDelay:2.0 beIndeter:NO];
        return;
    }
    NSLog(@"=======%@",_merchatNameField.text);
    NSString *is_enable = _isNew?@"1":_editDataModel.is_enable;
    __weak typeof (self)weakself = self;
    [[PPHttpRequsetData shareInstance] requestStyle:Request_POST URL:@"/ShopApi/Album/albumSave" isactive:YES parameter:@{@"agent_album_id":_editDataModel.albumID.length>0?_editDataModel.albumID:@"0",@"is_enable":is_enable,@"album_name":_merchatNameField.text,@"album_cover":_imageurl} successBlock:^(PPHttpRequest *request, id responseObj) {
        
        [AlertUtils showToastWithMessage:@"保存成功" inView:nil hideAfterDelay:2.0 beIndeter:NO];
        [weakself.navigationController popViewControllerAnimated:YES];
        
    } failBlock:^(PPHttpRequest *request, NSString *errMsg) {
        [AlertUtils showToastWithMessage:errMsg inView:self.view hideAfterDelay:2.0 beIndeter:NO];
    }];

}

- (IBAction)delMerchatAction:(id)sender {
    
    __weak typeof (self)weakself = self;
    [[PPHttpRequsetData shareInstance] requestStyle:Request_POST URL:@"/ShopApi/Album/albumDel" isactive:YES parameter:@{@"album_ids":_editDataModel.albumID} successBlock:^(PPHttpRequest *request, id responseObj) {
        
        [AlertUtils showToastWithMessage:@"删除成功" inView:nil hideAfterDelay:2.0 beIndeter:NO];
        [weakself.navigationController popViewControllerAnimated:YES];
        
    } failBlock:^(PPHttpRequest *request, NSString *errMsg) {
        [AlertUtils showToastWithMessage:errMsg inView:self.view hideAfterDelay:2.0 beIndeter:NO];
    }];

}

#pragma  mark ========选择相片========
- (void)chooseImage{
    UIActionSheet *sheet;
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从相册选择", nil];
    }else{
        sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"从相册选择", nil];
    }
    sheet.tag = 225;
    [sheet showInView:self.view];
}

#pragma  mark ========UIActionSheetDelegate========
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(actionSheet.tag == 225){
        NSUInteger sourceType = 0;
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
            switch (buttonIndex) {
                case 0:
                    //取消
                    return;
                    break;
                case 1:
                    //相机
                    sourceType = UIImagePickerControllerSourceTypeCamera;
                    break;
                case 2:
                    //相册
                    sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    break;
                default:
                    break;
            }
        }else{
            if(buttonIndex==0){
                return;
            }else{
                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            }
        }
        
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.sourceType = sourceType;
        imagePickerController.navigationBar.translucent = NO;
        imagePickerController.allowsEditing = YES;
        [self presentViewController:imagePickerController animated:YES completion:nil];
    }
}

#pragma  mark ========UIImagePickerControllerDelegate========
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
    UIImage *newImg = [info objectForKey:UIImagePickerControllerEditedImage];
    
    [[PPHttpRequsetData shareInstance] uploadImages:Request_POST URL:@"/ShopApi/Public/imgUpload" isactive:NO imageData:@[newImg] imgfileName:@"img_file" parameter:@{@"img_path":@"GOODS/"} successBlock:^(PPHttpRequest *request, id responseObj) {
        
            [_showCoverImg sd_setImageWithURL:responseObj[@"url"] placeholderImage:[UIImage imageNamed:@"photo_bg12_default"]];
             _editDataModel.album_cover = responseObj[@"url"];
             _imageurl = responseObj[@"url"];
        } failBlock:^(PPHttpRequest *request, NSString *errMsg) {
            
            [AlertUtils showToastWithMessage:errMsg inView:self.view hideAfterDelay:2.0 beIndeter:NO];
        }];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

@end
